package com.briup.day22;

import java.util.Scanner;

/**
 * @author ThinkBook
 */
public class Test02_Finally {
    //典型 finally 面试题
    public static void main(String[] args) {
        int r = getNum(10,0);

        System.out.println("r: " + r);
    }

    public static int getNum(int a, int b) {
        int n = 0;
        try {
            n = a / b;
        }catch(Exception e) {
            e.printStackTrace();
            n = 20;
            //先建立返回通道，放入 n当前的值 20，在最终返回前，再去调用finally
            return n;
        }finally {
            System.out.println("in finally, n: " + n);		//20
            n = 30;
            System.out.println("in finally,最后 n: " + n);	//30
        }

        return n;
    }

    //finally基本用法
    public static void main01(String[] args) {
        //1.从键盘录入2个数
        Scanner sc = new Scanner(System.in);
        System.out.println("input two num: ");
        int a = sc.nextInt();
        int b = sc.nextInt();

        //2.求商
        try {
            int v = div(a, b);
            System.out.println("商: " + v);
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            System.out.println("in finally, 模拟释放资源，即将离开 try-catch 块!");
        }

        System.out.println("main 即将结束 ...");
    }

    //求商运算
    public static int div(int x, int y) {
        int n = x / y;
        return n;
    }

}
